﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EO;
using System.Data;
using ThangNM_LIB.FUNC;

namespace LoginSqlServer.LIB
{
    public class ConvertNew
    {
        public static string getNameEx(ColumnEO colTemp)
        {
            if (String.IsNullOrEmpty(colTemp.sDescription))
                return colTemp.sColumnName;
            else
                return colTemp.sDescription;
        }

        public static string ConvertDataTypeSQLtoCsharp(ColumnEO colTemp)
        {
            if (colTemp == null)
                return " ";
            String strTemp = colTemp.sSQL_DataType.Trim().ToLower();

            if (strTemp == "char" || strTemp == "nchar" || strTemp == "varchar" || strTemp == "nvarchar" || strTemp == "text" || strTemp == "ntext")
                return "String";

            if (strTemp == "tinyint" || strTemp == "smallint")
                return "Int16";

            if (strTemp == "int")
                return "Int32";

            if (strTemp == "bigint")
                return "Int64";

            if (strTemp == "float" || strTemp == "double")
                return "Double";

            if (strTemp == "decimal" || strTemp == "numeric" || strTemp == "smallmoney" || strTemp == "money")
                return "Decimal";

            if (strTemp == "smalldatetime" || strTemp == "datetime" || strTemp == "date")
                return "DateTime";

            if (strTemp == "real")
                return "Single";

            if (strTemp == "bit")
                return "Boolean";

            return "N/A";
        }

        public static TableEO getFull_Info_Column(DataTable dt)
        {
            TableEO objRet = new TableEO();
            List<ColumnEO> lstCol = new List<ColumnEO>();
            lstCol = (List<ColumnEO>)ThangNM_LIB.FUNC.ConvertEx.ConvertDataTableToListObject<ColumnEO>(dt, lstCol.GetType());
            String[] arrTemp;
            if (lstCol != null && lstCol.Count > 0)
            {
                foreach (ColumnEO item in lstCol)
                {
                    arrTemp = item.sDescription.Split('|');
                    item.sDescription = arrTemp[0];
                    if (arrTemp.Length >= 2)
                        item.iTypeControl = ConvertNew.GetTypeControl(arrTemp[1]);
                    item.sCSharp_DataType = ConvertNew.ConvertDataTypeSQLtoCsharp(item);
                    item.sSQL_DataLength = StringEx.Trim(item.sSQL_DataLength);
                    item.sSQL_DataType_Length = ConvertNew.getDataTypeWithLength(item);
                    item.sInput = String.Format(Constants.MS_SQL.INPUT, item.sColumnName, item.sSQL_DataType_Length);
                    if (!item.bTableAttack)
                        item.sWhere = String.Format(Constants.MS_SQL.WHERE, "a", item.sColumnName, item.sColumnName);
                    else
                        item.sWhere = String.Format(Constants.MS_SQL.WHERE, "b", item.sColumnName, item.sColumnName);
                    item.bPrimaryKey = item.sColumnName.ToUpper().StartsWith("PK_");
                    item.bForeignKey = item.sColumnName.ToUpper().StartsWith("FK_");
                    if (item.bPrimaryKey)
                    {
                        objRet.sWhereKey += String.Format(Constants.MS_SQL.WHERE_EX, item.sWhere);
                        objRet.sWhereListKey += String.Format(Constants.MS_SQL.WHERE_LIST_KEY, item.sColumnName, item.sColumnName);
                        objRet.sInputKey += String.Format(Constants.MS_SQL.INPUT_EX, item.sInput);
                    }
                    objRet.sInputAll += String.Format(Constants.MS_SQL.INPUT_EX, item.sInput);
                }
                objRet.lstColumn = lstCol;

                if (StringEx.Trim(objRet.sWhereKey).StartsWith("\n\t\t\t\tAND "))
                    objRet.sWhereKey = objRet.sWhereKey.Substring(4);
                if (StringEx.Trim(objRet.sWhereListKey).StartsWith(" AND "))
                    objRet.sWhereListKey = objRet.sWhereListKey.Substring(4);
                if (StringEx.Trim(objRet.sInputKey).StartsWith(" ,"))
                    objRet.sInputKey = objRet.sInputKey.Substring(2);
                if (StringEx.Trim(objRet.sInputAll).StartsWith(" ,"))
                    objRet.sInputAll = objRet.sInputAll.Substring(2);
            }

            String abc = ThangNM_LIB.FUNC.JsonEx.Object2Json(objRet);
            return objRet;
        }

        public static String getDataTypeWithLength(ColumnEO colTemp)
        {
            String sSQL_DataType = StringEx.Trim(colTemp.sSQL_DataType).ToLower();
            if (StringEx.Trim(colTemp.sSQL_DataLength) == "-1")
                return colTemp.sSQL_DataType.ToUpper() + "(MAX)";
            else if (
                    1 != 1
                    || sSQL_DataType == "bigint"
                    || sSQL_DataType == "bit"
                    || sSQL_DataType == "date"
                    || sSQL_DataType == "datetime"
                    || sSQL_DataType == "datetime2"
                    || sSQL_DataType == "datetimeoffset"
                    || sSQL_DataType == "decimal"
                    || sSQL_DataType == "float"
                    || sSQL_DataType == "geography"
                    || sSQL_DataType == "geometry"
                    || sSQL_DataType == "hierarchyid"
                    || sSQL_DataType == "image"
                    || sSQL_DataType == "int"
                    || sSQL_DataType == "money"
                    || sSQL_DataType == "ntext"
                    || sSQL_DataType == "numeric"
                    || sSQL_DataType == "real"
                    || sSQL_DataType == "smalldatetime"
                    || sSQL_DataType == "smallint"
                    || sSQL_DataType == "smallmoney"
                    || sSQL_DataType == "sql_variant"
                    || sSQL_DataType == "text"
                    || sSQL_DataType == "timestamp"
                    || sSQL_DataType == "time"
                    || sSQL_DataType == "tinyint"
                    || sSQL_DataType == "uniqueidentifier"
                    || sSQL_DataType == "xml"
                )
                return colTemp.sSQL_DataType.ToUpper();
            else if (
                    1 != 1
                    || sSQL_DataType == "binary"
                    || sSQL_DataType == "char"
                    || sSQL_DataType == "nchar"
                    || sSQL_DataType == "nvarchar"
                    || sSQL_DataType == "varbinary"
                    || sSQL_DataType == "varchar"
                )
                return colTemp.sSQL_DataType.ToUpper() + "(" + colTemp.sSQL_DataLength + ")";
            return String.Empty;
        }

        public static Boolean CheckIsNumber(ColumnEO colTemp)
        {
            if (colTemp == null)
                return false;
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                case "smalldatetime":
                case "datetime":
                case "date":
                case "bit":
                    return false;
                    break;

                case "tinyint":
                case "smallint":
                case "int":
                case "bigint":
                case "float":
                case "double":
                case "decimal":
                case "numeric":
                case "smallmoney":
                case "money":
                case "real":
                    return true;
                    break;

                default:
                    return false;
                    break;
            }
        }

        public static string ConvertDataTypeSQLtoCssClass(ColumnEO colTemp)
        {
            if (colTemp == null)
                return " ";
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                    return "GridItemText";
                    break;

                case "tinyint":
                case "smallint":
                case "int":
                case "bigint":
                case "float":
                case "double":
                case "decimal":
                case "numeric":
                case "smallmoney":
                case "money":
                case "real":
                    return "GridItemNumber";
                    break;

                case "smalldatetime":
                case "datetime":
                case "date":
                case "bit":
                    return "GridItemDate";
                    break;

                default:
                    return "GridItemText";
                    break;
            }
        }

        public static string ConvertDataTypeSQLtoHtml5(ColumnEO colTemp)
        {
            if (colTemp == null)
                return " ";
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                case "bit":
                    if (colTemp.sColumnName.Trim().ToLower().Contains("email"))
                        return "required Html5=\"email\" pattern=\"[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,3}$\" title=\"ví dụ: example@company.com\" MaxLength=\"" + ConvertDataLengthSQLtoMaxLength(colTemp) + "\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("url") || colTemp.sColumnName.Trim().ToLower().Contains("link"))
                        return "required Html5=\"url\" pattern=\"https?://.+\" title=\"url phải bắt đầu bằng http:// hoặc https:// ví dụ: http://nguyenmanhthang.net\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("tel") || colTemp.sColumnName.Trim().ToLower().Contains("phone"))
                        return "required Html5=\"tel\" title=\"ví dụ: 0909123456\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("tude"))  //Latitude/Longitude
                        return "required pattern=\"[-+]?\\d{1,3}\\.\\d+\" title=\"ví dụ: 7.013668, 178.945313\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("ipv4"))    //IPv4
                        return "required pattern=\"\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\" title=\"ví dụ: 192.168.1.1\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("ipv6"))    //IPv6
                        return "required pattern=\"((^|:)([0-9a-fA-F]{0,4})){1,8}$\" title=\"ví dụ: 192.168.1.1\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("pass"))
                        return "required pattern=\"(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}\" title=\"Mật khẩu phải chứa ít nhất 1 ký tự chữ hoa, chữ thường, số và lớn hơn 8 ký tự\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("domain"))
                        return "required pattern=\"^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$\" title=\"ví dụ: abc.xyz\" ";
                    else if (colTemp.sColumnName.Trim().ToLower().Contains("md5"))
                        return "required pattern=\"[0-9a-fA-F]{32}\" title=\"ví dụ: d41d8cd98f00b204e9800998ecf8427e\" ";
                    else
                        return "required ";
                    break;

                case "tinyint":
                    return "required Html5=\"number\" pattern=\"[0-9]+\" min=\"0\" max=\"255\" ";
                    break;
                case "smallint":
                    return "required Html5=\"number\" pattern=\"[-+]?[0-9]+\" min=\"-32768\" max=\"32767\" ";
                    break;
                case "int":
                    return "required Html5=\"number\" pattern=\"[-+]?[0-9]+\" min=\"-2147483648\" max=\"2147483647\" ";
                    break;
                case "bigint":
                    return "required Html5=\"number\" pattern=\"[-+]?[0-9]+\" min=\"-9223372036854775808\" max=\"9223372036854775807\" ";
                    break;
                case "float":
                case "double":
                case "decimal":
                case "numeric":
                case "smallmoney":
                case "money":
                case "real":
                    return "required Html5=\"number\" step=\"any\" pattern=\"[-+]?[0-9]*[.,]?[0-9]+\" title=\"ví dụ: -152.51552\" ";
                    break;

                case "smalldatetime":
                case "datetime":
                case "date":
                    return "required Html5=\"datetime-local\" ";
                    break;
                case "uniqueidentifier":
                    return "required pattern=\"^[0-9A-Fa-f]{8}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{4}\\-[0-9A-Fa-f]{12}$\" title=\"ví dụ: 0a5560af-6046-459c-a83e-0d207863e4f5\" ";
                    break;
                default:
                    return "required ";
                    break;
            }
        }

        public static string FormatDataTypeSQLtoHtml5(ColumnEO colTemp, Boolean isTextBox)
        {
            if (colTemp == null)
                return " ";
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                case "bit":
                case "tinyint":
                case "smallint":
                case "int":
                case "bigint":
                    return "";
                    break;

                case "float":
                case "double":
                case "decimal":
                case "numeric":
                case "smallmoney":
                case "money":
                case "real":
                    return ", \"{0:###,##0.####}\"";
                    break;

                case "smalldatetime":
                case "datetime":
                case "date":
                    if (isTextBox)
                        return ", \"{0:yyyy-MM-ddTHH:mm:ss}\"";
                    else
                        return ", \"{0:dd/MM/yyyy HH:mm:ss}\"";
                    break;

                default:
                    return "";
                    break;
            }
        }

        public static string FormatDataTypeSQLForColumnGrid(ColumnEO colTemp)
        {
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar":
                case "text":
                case "ntext":
                case "bit":
                case "tinyint":
                case "smallint":
                case "int":
                case "bigint":
                    return "";
                    break;

                case "float":
                case "double":
                case "decimal":
                case "numeric":
                case "smallmoney":
                case "money":
                case "real":
                    return " DataFormatString=\"{0:###,##0.####}\" ";
                    break;

                case "smalldatetime":
                case "datetime":
                case "date":
                    return " DataFormatString=\"{0:dd/MM/yyyy HH:mm}\" ";

                default:
                    return "";
                    break;
            }
        }

        public static string ConvertDataLengthSQLtoMaxLength(ColumnEO colTemp)
        {
            if (colTemp == null)
                return "0";
            Int32 iTemp = 0;
            if (Int32.TryParse(Convert.ToString(colTemp.sSQL_DataLength), out iTemp))
                if (iTemp > 0)
                    return iTemp.ToString();

            return Int32.MaxValue.ToString();

        }

        public static string ConvertDataTypeToStyle(ColumnEO colTemp)
        {
            if (colTemp == null)
                return " ";
            switch (colTemp.sSQL_DataType.Trim().ToLower())
            {
                case "char":
                case "nchar":
                case "varchar":
                case "nvarchar": return "GridItemText"; break;
                case "int":
                case "smallint":
                case "tinyint":
                case "bigint":
                case "float":
                case "real":
                case "decimal": return "GridItemNumber"; break;
                case "datetime": return "GridItemDate"; break;
                default: return "N/A"; break;
            }
        }

        //public static string DefaultValue(string s)
        //{
        //    if (s == "char" || s == "nchar" || s == "varchar" || s == "nvarchar" || s == "text" || s == "ntext")
        //        return "\"\"";

        //    if (s == "tinyint")
        //        return "Byte";

        //    if (s == "smallint")
        //        return "Convert.ToInt16(0)";

        //    if (s == "int")
        //        return "0";

        //    if (s == "bigint")
        //        return "0";

        //    if (s == "float" || s == "double")
        //        return "0";

        //    if (s == "decimal" || s == "numeric" || s == "smallmoney" || s == "money")
        //        return "0";

        //    if (s == "smalldatetime" || s == "datetime" || s == "date")
        //        return "DateTime.MinValue ";

        //    if (s == "real")
        //        return "Single";

        //    if (s == "bit")
        //        return "false";

        //    return "N/A";
        //}

        public static Int16 GetTypeControl(String sInput)
        {
            Int16 iRet = 0;
            sInput = Convert.ToString(sInput).Trim().ToUpper();
            switch (sInput)
            {
                case "TXT1": iRet = 11; break;        //TextBox
                case "TXT2": iRet = 12; break;      //TextBox - MultiLine
                case "TXT3": iRet = 13; break;      //CKEditorControl
                case "TXT4": iRet = 14; break;      //TextBox - KeyValue
                case "TXT5": iRet = 15; break;

                case "DDL": iRet = 21; break;       //DropDownList

                case "LBX": iRet = 31; break;      //ListBox
                case "LBT": iRet = 31; break;      //ListBox - Multiple (Tag)

                case "CHK": iRet = 41; break;      //CheckBox
                case "CBL": iRet = 42; break;      //CheckBoxList

                case "FUD": iRet = 51; break;      //FileUpload

                case "HFD": iRet = 61; break;      //HiddenField

                case "RBN": iRet = 71; break;      //RadioButton
                case "RBL": iRet = 72; break;      //RadioButtonList
                default:
                    iRet = 0;
                    break;
            }
            return iRet;
        }
    }
}
